<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/12
 * Time: 13:48
 */
namespace Service\Model;
use IDCU\Model\RelationModel;

class OrderModel extends RelationModel
{
    public function getLst($pageSize=10,$tel=false){
        $key = I('keyword');
        $selectType = I('selectType');
        $community_id = I('community_id');
        $id = I('id');
        $step = I('step');

        $where = whereTime('createtime');
        if($selectType==1 && $key){
            if(preg_match("/^\d+$/", $key)) {
                $map['tel'] = array('like', "%$key%");
            }else {
                $map['realname'] = array('like', "%$key%");
            }
        }elseif($selectType==2 && $key){
            $_data = M('User')->where(['real_name'=>['like',"%$key%"]])->select();
            if(!$_data)
                $where['id'] = 9999999;
            $str = '';
            foreach ($_data as $v) {
                if($str)
                    $str .= ','.$v['id'];
                else
                    $str = $v['id'];
            }
            $map['service_man'] = array('in', $str);
        }elseif($selectType==3 && $key){
            $_data = M('User')->where(['real_name'=>['like',"%$key%"]])->select();
            if(!$_data)
                $where['id'] = 9999999;
            $str = '';
            foreach ($_data as $v) {
                if($str)
                    $str .= ','.$v['id'];
                else
                    $str = $v['id'];
            }
            $map['handler_man'] = array('in', $str);
        }elseif($selectType==4 && $key){
            $map['address'] = array('like', "%$key%");
        }elseif($selectType==5 && $key){
            $map['info'] = array('like', "%$key%");
        }elseif($selectType==6 && $key){
            $map['serialno'] = array('like', "%$key%");
        }
        if($map){
            $where['_complex'] = $map;
            $map['_logic'] = 'or';
        }
        if(!power_confirm() && !chckRole(4))
            $map['service_man'] = session('authId');
        if(chckRole(4))
            $where['handler_man'] = session('authId');
        if($community_id)
            $where['community_id'] = $community_id;
        if($id)
            $where['id'] = $id;
        if($step && $step!=10)
            $where['step'] = $step;
        if($tel)
            $where['tel'] = $tel;
        else{
            $tel = I('tel');
            if($tel) {
                $tel = preg_replace('/(0*)(1[^0])(\d{9})/', '$2$3', $tel);
                $map['tel'] = $tel;
                $map['_logic'] = 'OR';
                $where['_complex'] = $map;
            }
        }
//        myDump($where);
//        dd($where);
        /************************************* 翻页 ****************************************/
        $count = $this->where($where)->count();
        $page = new \ZL\Page($count, $pageSize);
        $data['page'] = $page->show();
        /************************************** 取数据 ******************************************/
        $orderData = $this->where($where)->limit($page->limit.','.$page->listRows)->order('id desc')->select();
        $orderData = $this->strChange($orderData);
        $data['data'] = $orderData;
        return $data;
    }
    public function snGenerate($community_id){
        $community = M('Community')->find($community_id);
        $crmId = $community['no'];

        $sn = date("ymd") .$crmId ."01" . rand(1000, 9999);
        return $sn;
    }

    public function strChange($orderData){
        foreach ($orderData as $k=>&$v) {
            switch ($v['step']){
                case 1:
                    $v['step_title'] = '未处理';
                    break;
                case 2:
                    $v['step_title'] = '完成';
                    break;
                case 3:
                    $v['step_title'] = '未完成';
                    break;
                case -1:
                    $v['step_title'] = '已取消';
                    break;
            }
            $v['service_name'] = M('User')->find($v['service_man'])['real_name'];
            $v['handler_name'] = M('User')->find($v['handler_man'])['real_name'];
            $v['type_name'] = M('RepairType')->where(['id'=>$v['type']])->find()['name'];
            $v['community_name'] = M('Community')->where(['id'=>$v['community_id']])->find()['name'];
            $v['create_time'] = date('Y-m-d H:i:s',$v['createtime']);
        }
        return $orderData;
    }

    public function adding(){
        $name = I('name');
        $parent = I('parent');
        $tel = I('tel');
        $type_id = I('type_id');
        $info = I('info');
        $addressData = explode(',',I('address'));
        $address = $addressData[0];
        $community_id = $addressData[1];
        $tel = preg_replace('/(0*)(1[^0])(\d{9})/','$2$3',$tel);


        $data['realname'] = $name;
        $data['tel'] = $tel;
        $data['serialno'] = $this->snGenerate($community_id);
        $data['type'] = $parent;
        $data['community_id'] = $community_id;
        $data['entry_id'] = $type_id;
        $data['entry_name'] = M('RepairType')->find($type_id)['name'];
        $data['address'] = $address;
        $data['step'] = 1;
        $data['info'] = $info;
        $data['service_man'] = session('authId');
        $data['createtime'] = time();
        $ok = $this->add($data);
        if($ok){
            return true;
        }else{
            return false;
        }
    }

    public function done(){
        $id = I('id');
        $remark = I('remark');
        $data['id'] = $id;
        $data['remark'] = $remark;
        $data['step'] = 2;
        $data['updateTime'] = time();
        $ok = $this->data($data)->save();
        if($ok){
            $res['status'] = 1;
        }else{
            $res['status'] = false;
            $res['msg'] = $this->getError();
        }
        return $res;
    }
    public function appoint(){
        $id = I('id');
        $handler_man = I('handler_man');
        $data['id'] = $id;
        $data['handler_man'] = $handler_man;
        $data['updateTime'] = time();
        $ok = $this->data($data)->save();
        if($ok){
            $res['status'] = 1;
        }else{
            $res['status'] = false;
            $res['msg'] = $this->getError();
        }
        return $res;
    }
    public function unDone(){
        $id = I('id');
        $remark = I('remark');
        $data['id'] = $id;
        $data['remark'] = $remark;
        $data['step'] = 3;
        $data['updateTime'] = time();
        $ok = $this->data($data)->save();
        if($ok){
            $res['status'] = 1;
        }else{
            $res['status'] = false;
            $res['msg'] = $this->getError();
        }
        return $res;
    }
    public function offing(){
        $id = I('id');
        $remark = I('remark');
        $data['id'] = $id;
        $data['remark'] = $remark;
        $data['step'] = -1;
        $data['updateTime'] = time();
        $ok = $this->data($data)->save();
        if($ok){
            $res['status'] = 1;
        }else{
            $res['status'] = false;
            $res['msg'] = $this->getError();
        }
        return $res;
    }
}